package hwOne2;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

/**
 * 作业 1.2.1
 */
public class Point2D {
    private double x;
    private double y;
    private double r;
    public Point2D(double x1, double y1) {
        x = x1;
        y = y1;
    }
    public double distanceTo(Point2D that) {
        double dx = this.x - that.x; // x 坐标差
        double dy = this.y - that.y; // y 坐标差
        return Math.sqrt(dx * dx + dy * dy); // 欧几里得距离公式
    }

    void draw() {
        StdDraw.point(this.x, this.y);
    }

    public static void main(String [] args) {
        if (args.length != 1) {
            System.out.println("请提供一个整数作为参数！");
            return;
        }

        int N = Integer.parseInt(args[0]);
        StdOut.println("N:" + N);

        // 创建一个数组存储 Point2D 对象
        Point2D[] points = new Point2D[N];

        // 生成 N 个随机点
        for (int i = 0; i < N; i++) {
            double x = StdRandom.uniformDouble(0.0, 1.0); // 随机生成 x 坐标 [0, 1)
            double y = StdRandom.uniformDouble(0.0, 1.0); // 随机生成 y 坐标 [0, 1)
            points[i] = new Point2D(x, y);
        }

        StdDraw.setCanvasSize(800, 800);
        StdDraw.setXscale(0, 1);
        StdDraw.setYscale(0, 1);
        StdDraw.setPenRadius(0.01);
        for (Point2D point : points) {
            point.draw();
        }

        double minDistance = Double.POSITIVE_INFINITY; // 初始化为正无穷
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                double distance = points[i].distanceTo(points[j]);
                if (distance < minDistance) {
                    minDistance = distance;
                }
            }
        }
        System.out.println("最短距离: " + minDistance);
    }
}
